package com.merlin.meta.app.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.merlin.framework.core.bean.PageBean;
import com.merlin.framework.core.bean.Result;
import com.merlin.meta.app.common.MetaAddress;
import com.merlin.meta.app.vo.MetaDomainAddVo;
import com.merlin.meta.app.vo.MetaDomainUpdateVo;
import com.merlin.meta.app.vo.MetaDomainVo;

@Service("metaDomainService")
public class MetaDomainService {
	@Autowired
	private RestTemplate restTemplate;
		/**
		 * 
		* @Title：createDomain 
		* @Description：领域信息新增服务
		* @param ：@param metaDomainAddVo
		* @param ：@param headers
		* @param ：@return 
		* @return ：Result<String> 
		* @throws
		 */
	public Result<String> createDomain(MetaDomainAddVo metaDomainAddVo, HttpHeaders headers) {
	
		HttpEntity<?> entity = new HttpEntity<>(metaDomainAddVo, headers);
		ResponseEntity<Result<String>> response;
		try {
			response = restTemplate.exchange(MetaAddress.createDomain, HttpMethod.POST, entity,
					new ParameterizedTypeReference<Result<String>>() {
					});
		} catch (Exception e) {
			return new Result<String>("3");
		}
		return response.getBody();
	}

     /**
      * 
     * @Title：deleteDomain 
     * @Description：领域信息删除服务
     * @param ：@param code
     * @param ：@param headers
     * @param ：@return 
     * @return ：Result<String> 
     * @throws
      */
	public Result<String> deleteDomain(String code, HttpHeaders headers) {
		HttpEntity<?> entity = new HttpEntity<>(null, headers);
		ResponseEntity<Result<String>> response = restTemplate.exchange(MetaAddress.deleteDomain,
				HttpMethod.DELETE, entity, new ParameterizedTypeReference<Result<String>>() {
				},code);
		return response.getBody();
	}
	 /**
	  * 
	 * @Title：alterDomain 
	 * @Description：领域更新服务
	 * @param ：@param metaDomainUpdateVo
	 * @param ：@param headers
	 * @param ：@return 
	 * @return ：Result<String> 
	 * @throws
	  */
	public Result<String> alterDomain(MetaDomainUpdateVo metaDomainUpdateVo, HttpHeaders headers) {
		HttpEntity<?> entity = new HttpEntity<>(metaDomainUpdateVo, headers);
		ResponseEntity<Result<String>> response = restTemplate.exchange(MetaAddress.alterDomain,
				HttpMethod.PUT, entity, new ParameterizedTypeReference<Result<String>>() {
				},metaDomainUpdateVo.getCode());
		return response.getBody();
	}	
	
    /**
     * 
    * @Title：searchDomains 
    * @Description：领域信息列表查询服务
    * @param ：@param objectCd
    * @param ：@param pageBean
    * @param ：@param headers
    * @param ：@return 
    * @return ：Result<List<MetaDomainVo>> 
    * @throws
     */
	public Result<List<MetaDomainVo>> searchDomains(PageBean pageBean,
			HttpHeaders headers) {
		//拼接请求路径
				String url = "?1=1";
				if(pageBean != null){
		    		if(pageBean.getNumber() != null){
		    			url += "&number=" + pageBean.getNumber();
		    		}
		    		if(pageBean.getSize() != null){
		    			url += "&size=" + pageBean.getSize();
		    		}
		    	}
		HttpEntity<List<MetaDomainVo>> entity = new HttpEntity<>(null, headers);
		ResponseEntity<Result<List<MetaDomainVo>>> response = null;
		try {
			response = restTemplate.exchange(MetaAddress.searchDomains+url,
					HttpMethod.GET, entity, new ParameterizedTypeReference<Result<List<MetaDomainVo>>>() {
					});
		} catch (Exception e) {
			return new Result<List<MetaDomainVo>>("3");
		}
		return response.getBody();
	}
   /**
    * 
   * @Title：getDomain 
   * @Description：单记录查询领域信息
   * @param ：@param code
   * @param ：@param headers
   * @param ：@return 
   * @return ：Result<MetaDomainVo> 
   * @throws
    */
	public Result<MetaDomainVo> getDomain(String code,HttpHeaders headers) {
		HttpEntity<?> entity = new HttpEntity<>(null, headers);
		ResponseEntity<Result<MetaDomainVo>> response = restTemplate.exchange(MetaAddress.getDomain,
				HttpMethod.GET, entity, new ParameterizedTypeReference<Result<MetaDomainVo>>() {
				},code);
		return response.getBody();
	}
}
